//
//  SoundController.m
//  Staff
//
//  Created by steve on 14-12-23.
//
//

#import "SoundController.h"

static const char* const CODE_BOOK = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@";
#define TOKEN_COUNT 24

ESVoid onSinVoiceRecognizerStart(ESVoid* cbParam) {
    NSLog(@"onSinVoiceRecognizerStart");
    SoundController* vc = (__bridge SoundController*)cbParam;
    vc->mResultCount = 0;
}

ESVoid onSinVoiceRecognizerToken(ESVoid* cbParam, ESInt32 index) {
    NSLog(@"onSinVoiceRecognizerToken, index:%d", index);
    SoundController* vc = (__bridge SoundController*)cbParam;
    vc->mResults[vc->mResultCount++] = index;
}

ESVoid onSinVoiceRecognizerEnd(ESVoid* cbParam, ESInt32 result) {
    NSLog(@"onSinVoiceRecognizerEnd, result:%d", result);
    SoundController* vc = (__bridge SoundController*)cbParam;
    [vc onRecogToken:vc];
}

ESVoid onSinVoicePlayerStart(ESVoid* cbParam) {
    NSLog(@"onSinVoicePlayerStart, start");
    SoundController* vc = (__bridge SoundController*)cbParam;
    [vc onPlayData:vc];
    NSLog(@"onPlayData, end");
}

ESVoid onSinVoicePlayerStop(ESVoid* cbParam) {
    NSLog(@"onSinVoicePlayerStop");
}

SinVoicePlayerCallback gSinVoicePlayerCallback = {onSinVoicePlayerStart, onSinVoicePlayerStop};
SinVoiceRecognizerCallback gSinVoiceRecognizerCallback = {onSinVoiceRecognizerStart, onSinVoiceRecognizerToken, onSinVoiceRecognizerEnd};

@interface SoundController ()

@property (nonatomic, weak) IBOutlet UILabel *showMessage;
@property (nonatomic, weak) IBOutlet UITextField *input;

@end

@implementation SoundController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    mSinVoicePlayer = SinVoicePlayer_create("com.sinvoice.test", "", &gSinVoicePlayerCallback, (__bridge ESVoid *)(self));
    mSinVoiceRecorder = SinVoiceRecognizer_create("com.sinvoice.test", "", &gSinVoiceRecognizerCallback, (__bridge ESVoid *)(self));
}

-(void)onPlayData:(SoundController*)data
{
    NSThread* curThrd =[NSThread currentThread];
    NSLog(@"onPlayData, thread:%@",curThrd);
    [self performSelectorOnMainThread:@selector(updateUI:) withObject:data waitUntilDone:FALSE];
}

-(void)updateUI:(SoundController*)data
{
    NSThread* curThrd =[NSThread currentThread];
    NSLog(@"updateUI, thread:%@",curThrd);
    
    //    NSMutableString* str = [[NSMutableString alloc]init];
    //    for ( int i = 0; i < TOKEN_COUNT; ++i ) {
    //        [str appendFormat:@"%c", CODE_BOOK[data->mRates[i]]];
    //    }
    char ch[100] = { 0 };
    for ( int i = 0; i < mPlayCount; ++i ) {
        ch[i] = (char)data->mRates[i];
    }
    
    //    NSString* str = [NSString stringWithCString:ch encoding:NSUTF8StringEncoding];
    
    //    _mPlayLabel.text = str;
}

-(void)onRecogToken:(SoundController*)data
{
    NSThread* curThrd =[NSThread currentThread];
    NSLog(@"onRecordData, thread:%@",curThrd);
    [self performSelectorOnMainThread:@selector(updateRecordUI:) withObject:data waitUntilDone:FALSE];
}

-(void)updateRecordUI:(SoundController*)data
{
    NSThread* curThrd =[NSThread currentThread];
    NSLog(@"updateUI, thread:%@",curThrd);
    
    char ch[100] = { 0 };
    for ( int i = 0; i < mResultCount; ++i ) {
        ch[i] = (char)data->mResults[i];
    }
    
    NSString* str = [NSString stringWithCString:ch encoding:NSUTF8StringEncoding];
    
    //    NSMutableString* str = [[NSMutableString alloc]init];
    //    for ( int i = 0; i < TOKEN_COUNT; ++i ) {
    //        [str appendFormat:@"%c", data->mResults[i]];
    //    }
    
    
    _showMessage.text = str;
}

- (IBAction)startPlay:(UIButton *)sender {
    NSLog(@"push start play");
    int index = 0;
    //    srand((int)time(0));
    NSString* xx = _input.text;
    const char* str = [xx cStringUsingEncoding:NSUTF8StringEncoding];
    
    mPlayCount = (int)strlen(str);
    //    char* str = PLAY_STRING;
    while ( index < mPlayCount) {
        mRates[index] = str[index];
        ++index;
    }
    SinVoicePlayer_play(mSinVoicePlayer, mRates, mPlayCount);
    
    //    while ( index < TOKEN_COUNT) {
    //        int rnd = rand();
    //        mRates[index] = 60 * (double)rnd / RAND_MAX;
    //        ++index;
    //    }
    //    SinVoicePlayer_play(mSinVoicePlayer, mRates, TOKEN_COUNT);
}

- (IBAction)stopPlay:(UIButton *)sender {
    NSLog(@"push stop play");
    SinVoicePlayer_stop(mSinVoicePlayer);
}

- (IBAction)startRecord:(UIButton *)sender {
    NSLog(@"push start record");
    SinVoiceRecognizer_start(mSinVoiceRecorder, TOKEN_COUNT);
}

- (IBAction)stopRecord:(UIButton *)sender {
    NSLog(@"push stop record");
    SinVoiceRecognizer_stop(mSinVoiceRecorder);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
